package com.easy.ui.login;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.easy.MslApp;
import com.easy.R;
import com.easy.base.activity.BaseActivity;
import com.easy.base.util.Util;
import com.easy.tool.ToastUtils;
import com.easy.ui.config.ConfigActivity;
import com.easy.ui.main.MainActivity;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallback;
import com.permissionx.guolindev.callback.ExplainReasonCallbackWithBeforeParam;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginModel, LoginView, LoginPresent> implements LoginView{
    private String tag = "LoginActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.ivu)
    ImageView ivu;
    @Bind(R.id.login_id)
    EditText loginId;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.cb_rp)
    CheckBox cbRp;
    @Bind(R.id.loginbt)
    Button loginbt;
    @Bind(R.id.configbt)
    Button configbt;
    @Bind(R.id.tvr)
    TextView tvr;
    @Bind(R.id.iv)
    ImageView iv;
    boolean flag = false;
    String cid,user,pw;

    public static String getVersionName(Context mContext) {
        String versionName = "0.0";
        try {
            versionName = mContext.getPackageManager().
                    getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } finally {
            return versionName;
        }
    }

    private void testEasyPermission() {
        PermissionX.init(this)
                .permissions(Manifest.permission.CAMERA)
                .explainReasonBeforeRequest()
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, "权限很重要", "我已明白");
                    }
                })
                .onExplainRequestReason(new ExplainReasonCallback() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList) {
                        scope.showRequestReasonDialog(deniedList, "即将重新申请的权限是程序必须依赖的权限", "我已明白", "取消");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白", "取消");
                    }
                })
                .request(new RequestCallback() {
            @Override
            public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                if (allGranted) {
                    Toast.makeText(LoginActivity.this, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "您拒绝了如下权限：" + deniedList, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tvTitle.setText("登录");
        loginId.setText(MslApp.sSharedPreferences.getString(Util.userk, ""));
        loginP.setText(MslApp.sSharedPreferences.getString(Util.pwk, ""));
        tvr.setText("版本号:v" + getVersionName(this));
        testEasyPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadImage();
    }

    @Override
    public LoginModel createModel() {
        return new LoginModelImpl();
    }

    @Override
    public LoginView createView() {
        return this;
    }

    @Override
    public LoginPresent createPresenter() {
        return new LoginPresent();
    }

    @Override
    public void login() {
        ToastUtils.showSingleToast(this,"登录成功");
        Intent intent = new Intent();
        intent.setClass(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    @Override
    public void showpic()
    {
        Glide.with(this).load(MslApp.sSharedPreferences.getString(Util.url_image,"")).into(iv);
    }

    @OnClick({R.id.iv_title_left, R.id.cb_rp, R.id.loginbt, R.id.configbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.cb_rp:
                flag = !flag;
                break;
            case R.id.loginbt:
                if (MslApp.sSharedPreferences.getString(Util.cidk, "").length() == 0) {
                    ToastUtils.showSingleToast(this, "请配置企业编号");
                    return;
                }
                cid = MslApp.sSharedPreferences.getString(Util.cidk, "");
                user = loginId.getText().toString();
                pw = loginP.getText().toString();
                if (user.length() == 0) {
                    ToastUtils.showSingleToast(this, "请填写用户名");
                    return;
                }
                if (pw.length() == 0) {
                    ToastUtils.showSingleToast(this, "请填写密码");
                    return;
                }
                presenter.login(user,pw);
                break;
            case R.id.configbt:
                Intent intent1 = new Intent();
                intent1.setClass(this, ConfigActivity.class);
                startActivity(intent1);
                break;
        }
    }






}
